package com.fms.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fms.common.utils.JwtUtils;
import com.fms.common.vo.Result;
import com.fms.sys.entity.Schedule;
import com.fms.sys.service.IScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 日程管理控制器
 * </p>
 *
 * @author Jinfeng
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private IScheduleService scheduleService;

    /**
     * 分页查询日程列表
     */
    @GetMapping("/list")
    public Result<Map<String, Object>> getScheduleList(
            @RequestParam(value = "pageNo", defaultValue = "1") Long pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize,
            @RequestParam(value = "title", required = false) String title) {

        Long userId = JwtUtils.getCurrentUserId();
        LambdaQueryWrapper<Schedule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Schedule::getCreateBy, userId);
        if (StringUtils.hasText(title)) {
            wrapper.like(Schedule::getTitle, title);
        }
        wrapper.orderByDesc(Schedule::getCreateTime);

        Page<Schedule> page = new Page<>(pageNo, pageSize);
        scheduleService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }

    /**
     * 新增日程
     */
    @PostMapping
    public Result<?> addSchedule(@RequestBody Schedule schedule) {
        if (schedule.getTitle() == null || schedule.getTitle().trim().isEmpty()) {
            return Result.fail("标题不能为空");
        }
        schedule.setCreateBy(JwtUtils.getCurrentUserId());
        scheduleService.save(schedule);
        return Result.success("新增日程成功");
    }

    /**
     * 修改日程
     */
    @PutMapping
    public Result<?> updateSchedule(@RequestBody Schedule schedule) {
        Schedule existingSchedule = scheduleService.getById(schedule.getId());
        if (existingSchedule == null || !existingSchedule.getCreateBy().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("无权修改此日程");
        }
        scheduleService.updateById(schedule);
        return Result.success("修改日程成功");
    }

    /**
     * 删除日程
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteSchedule(@PathVariable("id") Long id) {
        Schedule schedule = scheduleService.getById(id);
        if (schedule == null || !schedule.getCreateBy().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("日程不存在或无权删除");
        }
        scheduleService.removeById(id);
        return Result.success("删除日程成功");
    }

    /**
     * 获取日程详情
     */
    @GetMapping("/{id}")
    public Result<Schedule> getScheduleById(@PathVariable("id") Long id) {
        Schedule schedule = scheduleService.getById(id);
        if (schedule == null || !schedule.getCreateBy().equals(JwtUtils.getCurrentUserId())) {
            return Result.fail("日程不存在或无权查看");
        }
        return Result.success(schedule);
    }
}